package com.example.manage.controller;

import com.example.manage.bean.ParkBean;
import com.example.manage.bean.ParkBean;
import com.example.manage.bean.ResultBean;
import com.example.manage.mapper.ParkMapper;
import com.example.manage.mapper.ParkMapper;
import com.example.manage.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/park")
public class ParkController {

    @Autowired
    ParkMapper parkMapper;

    @RequestMapping("/addPark")
    @ResponseBody
    public ResultBean addPark(@RequestBody ParkBean parkBean) {
        List<ParkBean> beans = parkMapper.getParkByNumber(parkBean.getNumber());
        if (beans != null && beans.size()>0) {
            return ResultUtil.getFailBean("该编号已存在，请修改后提交");
        }
        int count = parkMapper.addPark(parkBean);
        return ResultUtil.getResultBean(count, "添加失败");
    }

    @RequestMapping("/updatePark")
    @ResponseBody
    public ResultBean updatePark(@RequestBody ParkBean parkBean) {
        int count = parkMapper.updatePark(parkBean);
        return ResultUtil.getResultBean(count, "修改失败");
    }

    @RequestMapping("/deletePark")
    @ResponseBody
    public ResultBean deletePark(@RequestBody Map<String, Integer> params) {
        int count = parkMapper.deletePark(params.get("id"));
        return ResultUtil.getResultBean(count, "删除失败");
    }

    @RequestMapping("/getPark")
    @ResponseBody
    public ResultBean getPark() {
        List<ParkBean> parkBeans = parkMapper.getPark();
        return ResultUtil.getSuccessBean(parkBeans);
    }

    @RequestMapping("/getParkByNumber")
    @ResponseBody
    public ResultBean getParkByNumber(@RequestParam("number") String number) {
        return ResultUtil.getSuccessBean(parkMapper.getParkByNumber(number));
    }

}
